package com.starsea.book.ui.newpage

import android.os.Bundle
import android.view.View
import com.starsea.book.R
import com.starsea.book.base.BaseFragment
import com.starsea.book.constant.PreferKey
import com.starsea.book.databinding.ActivitySettingBinding
import com.starsea.book.lib.dialogs.alert
import com.starsea.book.ui.config.ConfigActivity
import com.starsea.book.ui.config.ConfigTag
import com.starsea.book.ui.newpage.utils.DataCleanManager
import com.starsea.book.utils.LogUtils
import com.starsea.book.utils.putPrefBoolean
import com.starsea.book.utils.putPrefString
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2023-12-26 09 GMT+8
 * @desc :
 */
class SettingFragment() : BaseFragment(R.layout.activity_setting) {

    private val binding by viewBinding(ActivitySettingBinding::bind)

    override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
        LogUtils.e(">>> ", "onFragmentCreated")

        binding.rtvAboutUs.setOnClickListener {

            LogUtils.e(">>> ", "setOnClickListener")

            startActivity<ConfigActivity> {
                putExtra("configTag", ConfigTag.ABOUT_US_CONFIG)
            }
        }

        binding.rtvUserProfile.setOnClickListener {
            startActivity<ConfigActivity> {
                putExtra("configTag", ConfigTag.USER_PROFILE_CONFIG)
            }
        }

        binding.tvCacheSize.text = DataCleanManager.getTotalCacheSize(requireActivity())
        binding.rllCache.setOnClickListener {
            clearCache()
        }

        binding.rtvLogout.setOnClickListener {
            putPrefBoolean(PreferKey.isLogin, false)
            putPrefString(PreferKey.token, "")
            LoginActivity.start(requireContext())
        }

    }


    private fun clearCache() {
        requireContext().alert(
            titleResource = R.string.clear_cache,
            messageResource = R.string.sure_del
        ) {
            okButton {
                DataCleanManager.clearAllCache(requireContext())
                binding.tvCacheSize.text = "0Kb"
                toastOnUi(R.string.clear_cache_success)
            }
            noButton()
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }
}